<?php
/**
 * 异常基类
 * 主要重载异常打印函数
 *
 * @author edikud
 * @package Exception
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Ex extends Exception
{
	
    public static $called = FALSE;
	
    public function __construct($message, $code = 0)
    {
        $this->message = $message;
        $this->code = $code;
		
		/** 插件接口 */
		//HOOK {$plugged} | PLUG_Exception($this);
		
        if (!$plugged ) {
			
			if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && trim($_SERVER['HTTP_X_REQUESTED_WITH']) == 'XMLHttpRequest') {
				
			    header('Content-Type: application/json; charset=UTF-8', true);
			
		        $notice = array(
		            'code'   => $code,
			        'msg'    => $message,
			        'data'   => ''
		        );
			
	            // 防止 Ex 本身出现错误死循环
	            self::$called ? exit(Json::encode($notice)) : self::$called = TRUE;

			    exit(Json::encode($notice));
            }
		}
    }
}
